package com.danco.sn.entities;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.danco.sn.apientities.ICommunity;
import com.danco.sn.apientities.ICommunityMessage;
import com.danco.sn.apientities.INews;
import com.danco.sn.apientities.IUser;

@Entity
@Table(name = "community")
public class Community implements ICommunity{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_community")
	private Integer idCommunity;

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = User.class)
	@JoinColumn(name="id_moderator")
	private IUser moderator;

	@Column(name = "community_name")
	private String communityName;

	@Column(name = "date_create")
	@Temporal(value = TemporalType.DATE)
	private Date dateCreate;

	@Column(name = "community_city")
	private String communityCity;
	
	@ManyToMany(cascade = {CascadeType.ALL},targetEntity = User.class, fetch=FetchType.LAZY)
	@JoinTable(name="community_users",joinColumns={@JoinColumn(name="id_community")}, 
            inverseJoinColumns={@JoinColumn(name="id_user")})
	private List<IUser> members;
	
	@OneToMany(cascade = {CascadeType.ALL},targetEntity = CommunityMessage.class, fetch=FetchType.LAZY)
	@JoinTable(name="community_message", joinColumns={@JoinColumn(name="id_community")}, inverseJoinColumns={@JoinColumn(name="id_message")})
	private List<ICommunityMessage> listCommunityMessage;
	
	@OneToMany(cascade = {CascadeType.ALL},targetEntity = NewsCommunity.class, fetch=FetchType.LAZY)
	@JoinTable(name="news_community", joinColumns={@JoinColumn(name="id_community")}, inverseJoinColumns={@JoinColumn(name="id_news")})
	private List<INews> listNews;
		
	public List<INews> getListNews() {
		return listNews;
	}

	public void setListNews(List<INews> listNews) {
		this.listNews = listNews;
	}

	public void setMember(IUser user){
		members.add(user);
	}
	
	public List<ICommunityMessage> getListMessage() {
		return listCommunityMessage;
	}

	public void setListMessage(List<ICommunityMessage> listCommunityMessage) {
		this.listCommunityMessage = listCommunityMessage;
	}

	public List<IUser> getMembers() {
		return members;
	}

	public void setMembers(List<IUser> members) {
		this.members = members;
	}
	
	

	public void setModerator(IUser moderator) {
		this.moderator = moderator;
	}

	public IUser getModerator() {
		return moderator;
	}

	public Integer getIdCommunity() {
		return idCommunity;
	}

	public void setIdCommunity(Integer idCommunity) {
		this.idCommunity = idCommunity;
	}

	public String getCommunityName() {
		return communityName;
	}

	public void setCommunityName(String communityName) {
		this.communityName = communityName;
	}

	public Date getDateCreate() {
		return dateCreate;
	}

	public void setDateCreate(Date dateCreate) {
		this.dateCreate = dateCreate;
	}

	public String getCommunityCity() {
		return communityCity;
	}

	public void setCommunityCity(String communityCity) {
		this.communityCity = communityCity;
	}

}
